package com.cskaoyan.controller;

import com.cskaoyan.bean.BO.CheckoutBO;
import com.cskaoyan.bean.BO.WxCartCheckedBO;
import com.cskaoyan.bean.PO.MarketCart;
import com.cskaoyan.bean.VO.WxCartCheckoutVO;
import com.cskaoyan.bean.VO.WxCartIndexVO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.WxCartService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @ClassName WxCartController
 * @Description
 * @Author Lxz
 * @Date 2022-8-17 9:05
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    WxCartService cartService;


    @RequestMapping("goodscount")
    public BaseRespVo goodsCount() {
        Integer count = cartService.goodsCount();
        return BaseRespVo.ok(0, count, "成功");
    }

    @RequestMapping("add")
    public BaseRespVo add(@RequestBody MarketCart marketCart) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getSession().getAttribute("userId");
        marketCart.setUserId(userId);
        cartService.add(marketCart);
        Integer count = cartService.goodsCount();
        return BaseRespVo.ok(0, count, "成功");
    }


    @RequestMapping("index")
    public BaseRespVo index() {
        WxCartIndexVO index = cartService.index();
        return BaseRespVo.ok(0, index, "成功");
    }

    @RequestMapping("update")
    public BaseRespVo update(@RequestBody MarketCart marketCart) {
        cartService.update(marketCart);
        return BaseRespVo.ok(0, null, "成功");
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody WxCartCheckedBO wxCartCheckedBO) {
        cartService.delete(wxCartCheckedBO);
        WxCartIndexVO index = cartService.index();
        return BaseRespVo.ok(0, index, "成功");
    }

    @RequestMapping("checked")
    public BaseRespVo checked(@RequestBody WxCartCheckedBO wxCartCheckedBO) {
        cartService.checked(wxCartCheckedBO);
        WxCartIndexVO index = cartService.index();
        return BaseRespVo.ok(0, index, "成功");
    }


    @RequestMapping("fastadd")
    public BaseRespVo fastadd(@RequestBody MarketCart marketCart) {
        Subject subject = SecurityUtils.getSubject();
        Integer userId = (Integer) subject.getSession().getAttribute("userId");
        marketCart.setUserId(userId);
        cartService.fastadd(marketCart);
        Integer id = marketCart.getId();
        return BaseRespVo.ok(0, id, "成功");
    }

    @RequestMapping("checkout")
    public BaseRespVo checkout(CheckoutBO checkoutBO) {
        WxCartCheckoutVO checkout = cartService.checkout(checkoutBO);
        return BaseRespVo.ok(0, checkout, "成功");
    }

}
